<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 商品查找 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="queryParams.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表 -->
      <el-table :data="goods" border stripe>
        <el-table-column type="index"/>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
        <el-table-column label="创建时间" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" size="mini" icon="el-icon-edit" />
            <!-- 删除按钮 -->
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="delGoods(scope.row.goods_id)"/>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[1, 2, 5, 10]"
        :current-page="queryParams.pagenum"
        :page-size="queryParams.pagesize"
        :total="total"
        background
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品查找表单对象
      queryParams: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goods: [],
      total: 0,
      // 添加商品对话框
      addGoodsDialogVisible: false
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 分页获取商品列表
    async getGoodsList () {
      const { data } = await this.$http.get('goods', { params: this.queryParams })
      if (data.meta.status !== 200) {
        return this.$message.error('获取商品列表失败，' + data.meta.msg)
      }
      this.goods = data.data.goods
      this.total = data.data.total
    },
    // 页面大小改变事件
    changePageSize (newSize) {
      this.queryParams.pagesize = newSize
      this.getGoodsList()
    },
    // 页码改变事件
    changePage (newPage) {
      this.queryParams.pagenum = newPage
      this.getGoodsList()
    },
    // 删除商品
    async delGoods (id) {
      const res = await this.$confirm('确认删除该商品？',
        '确认删除', {
          confirmButtonText: '确定',
          calcelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
      if (res !== 'confirm') {
        return this.$message.info('取消删除')
      }
      const { data } = await this.$http.delete('goods/' + id)
      if (data.meta.status !== 200) {
        return this.$message.error('删除商品失败，' + data.meta.msg)
      }
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },
    // 跳转到添加商品页面
    showAddGoodsPage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
